import java.awt.*;
import java.awt.event.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;

import javax.swing.*;
import javax.swing.*;
import javax.swing.border.Border;


public class Ipbox extends JDialog implements ActionListener {
	
	public static JTextField inputBox;
	public static JButton debugBox;
	public static JButton okButton;
	public static JButton cancelButton;
	public static JLabel background;
	
	public static ImageIcon ok_button_up;
	public static ImageIcon ok_button_down;
	public static ImageIcon cancel_button_up;
	public static ImageIcon cancel_button_down;
	public static ImageIcon debug_button_on;
	public static ImageIcon debug_button_off;
	public static ImageIcon bgimage;
	public static int closeStatus;
	public static ClientInfo myInfo;
	
	public Ipbox(JFrame parent, ClientInfo currInfo)
	{
		setLayout(null);
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		setSize(410,400);
		
		myInfo = currInfo;
		
		background = new JLabel();
		okButton = new JButton();
		cancelButton = new JButton();
		inputBox = new JTextField("127.0.0.1");
		debugBox = new JButton();
		
		bgimage = new ImageIcon("images/ipdialogbg.jpg");
		cancel_button_up = new ImageIcon("images/cancel_button_up.jpg");
		cancel_button_down = new ImageIcon("images/cancel_button_down.jpg");
		ok_button_up = new ImageIcon("images/ok_button_up.jpg");
		ok_button_down = new ImageIcon("images/ok_button_down.jpg");
		debug_button_on = new ImageIcon("images/debug_button_on.jpg");
		debug_button_off = new ImageIcon("images/debug_button_off.jpg");
		
		inputBox.setBackground(Color.BLACK);
		inputBox.setForeground(Color.WHITE);
		inputBox.setBorder(null);
		
		okButton.setIcon(ok_button_up);
		okButton.setPressedIcon(ok_button_down);
		okButton.setBorderPainted(false);

		cancelButton.setIcon(cancel_button_up);
		cancelButton.setPressedIcon(cancel_button_down);
		cancelButton.setBorderPainted(false);
		
		debugBox.setIcon(debug_button_off);
		debugBox.setBorderPainted(false);
		
		background.setIcon(bgimage);
	
		okButton.setBounds(250,240,120,120);
		cancelButton.setBounds(15,240,120,120);
		debugBox.setBounds(15,170,200,50);
		inputBox.setBounds(40, 100, 200, 25);
		background.setBounds(0,0,600,400);
		
		debugBox.addActionListener(this);
		cancelButton.addActionListener(this);
		okButton.addActionListener(this);
		
		add(debugBox);
		add(okButton);
		add(cancelButton);
		add(inputBox);
		add(background);
		
		inputBox.selectAll();
		
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		JButton button = (JButton)arg0.getSource();
		InetAddress ia = null;
		if(button == okButton){
			try
			{
			myInfo.ipstring = inputBox.getText();
			ia.getByName(myInfo.ipstring);
			closeStatus = 1;
			dispose();
			}
			catch(UnknownHostException exp)
			{
				JOptionPane.showMessageDialog(this, 
						   "ERROR: Can't Parse Ip Address/Host Name." ,
						     "ERROR" + "", 
						       JOptionPane.ERROR_MESSAGE);

			}
		}
		else if(button == cancelButton){
			System.exit(0);
		}
		else if(button == debugBox){
			if(debugBox.getIcon() == debug_button_off){
				debugBox.setIcon(debug_button_on);
				myInfo.d_flag = true;
			}
			else
			{
				debugBox.setIcon(debug_button_off);
				myInfo.d_flag = false;
			}
		}
		
	}
	
	 public int getCloseStatus(){
		 return closeStatus;
	}
}
